﻿Public Class Cow
    Dim xCowOld As Integer
    Dim Health As Integer
    Dim xHungry As Boolean
    Dim xFeedCount As Integer
    Dim xProduced As Integer
    Dim xPos As Integer
    Public Property CowOld As Integer
        Get
            Return xCowOld
        End Get
        Set(value As Integer)
            xCowOld = value
        End Set
    End Property
    Public Property FeedCount As Integer
        Get
            Return xFeedCount
        End Get
        Set(value As Integer)
            xFeedCount = value
        End Set
    End Property
    Public Property Produced As Integer
        Get
            Return xProduced
        End Get
        Set(value As Integer)
            xProduced = value
        End Set
    End Property
    Public Property Pos As Integer
        Get
            Return xPos
        End Get
        Set(value As Integer)
            xPos = value
        End Set
    End Property
    Public Sub TimerCowHunger_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerCowHunger.Tick
        xHungry = True
        CowDamage()
    End Sub
    Public Sub CowDamage()
        HealthMeter.BackColor = Color.DarkRed
        HealthMeter.Height -= 5
        HealthMeter.Top += 5
        Health += 5
        If HealthMeter.Height <= 0 Then
            Me.Hide()
            'minus cow total
            Parent.Controls.OfType(Of Label)().ElementAt(4).Text -= 1
            If Parent.Controls.OfType(Of Label)().ElementAt(4).Text = 0 And Parent.Controls.OfType(Of Label)().ElementAt(3).Text = 0 Then
                Me.TimerBiten.Stop()
                Me.TimerCowHunger.Stop()
                Dim Obj As New Point
                Obj.PassedText = Parent.Controls.OfType(Of Label)().ElementAt(0).Text
                Obj.HighScore = Parent.Controls.OfType(Of Label)().ElementAt(9).Text
                Obj.Killeds = Convert.ToInt64(Parent.Controls.OfType(Of Label)().ElementAt(1).Text)

                Obj.Show()
            End If
            Me.Dispose()
        End If
    End Sub
    Public Sub CowFeeded()
        HealthMeter.BackColor = Color.ForestGreen
        HealthMeter.Height = 45
        HealthMeter.Top = 1
        xFeedCount += 1
        If Not CowOld = 3 Then
            Grow(xFeedCount)
        End If
    End Sub
    Public Sub Grow(ByVal FeedCount As Integer)
        If FeedCount >= 5 Then
            CowOld = 2
            Me.CowImage.Image = My.Resources.cow
            Me.TimerProduce.Enabled = True
        Else
            CowOld = 1
            Me.CowImage.Image = My.Resources.smallcow
        End If
    End Sub
    Public Sub HarvestMilk()
        'Disposing Object Milk
        Dim ObjMilk As Object
        For Each ObjMilk In Me.Controls
            If TypeOf ObjMilk Is Harvest Then
                ObjMilk.Dispose()
            End If
        Next
        'LabelScore
        If CowOld = 3 Then
            Parent.Controls.OfType(Of Label)().ElementAt(0).Text += 400
        Else
            Parent.Controls.OfType(Of Label)().ElementAt(0).Text += 200
        End If
        TimerProduce.Start()
    End Sub
    Private Sub TimerProduce_Tick(sender As Object, e As EventArgs) Handles TimerProduce.Tick
        TimerProduce.Interval = 5000
        For Each ObjMilk In Me.Controls
            If TypeOf ObjMilk Is Harvest Then
                ObjMilk.Dispose()
            End If
        Next
        Dim x As New Harvest
        'Adding handler for each x
        AddHandler x.Click, AddressOf HarvestMilk

        'Checking layed status and increment the layed value
        If Produced >= 10 Then
            CowOld = 3
        End If
        Produced += 1

        If CowOld = 3 Then
            x.ShowHarvest("MilkGold")
        Else
            x.ShowHarvest("Milk")
        End If
        Me.Controls.Add(x)
        TimerProduce.Stop()
    End Sub
    Private Sub CowImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CowImage.Click
        If xHungry = True Then
            CowFeeded()
            xHungry = False
        End If
    End Sub
    Private Sub Cow_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If CowOld >= 2 Then
            'set dis to false before open main.vb[design]
            Me.TimerProduce.Enabled = True
        Else
            Me.TimerProduce.Enabled = False
        End If
    End Sub

    Private Sub TimerBiten_Tick(sender As Object, e As EventArgs) Handles TimerBiten.Tick
        xHungry = True
        CowDamage()
    End Sub
End Class
